<?php
/**
 * @file
 * @brief implement Skills class
 */

/**
 * @brief Parse the skills.yaml file.
 *
 * This class is used every time we need a list of all skills
 * that exist in the game.
 */
class Skills {
	/**
	 * @brief returns all skills grouped by categories
	 * @param[in] $lang desired language of the results
	 * @param[out] $list two-dimensional array of skills grouped by categories
	 *
	 * This is used when the roster is generated. Ultimately, the template
	 * _javascript.tpl produces javascript-arrays, which are very important
	 * to the skill-handling in the javascript.
	 */
	public static function all_nested($lang=LANG) {
		$file = 'data/' . $lang . '/skills.yaml';
		$yaml = explode("\n", file_get_contents($file));

		$list = array();
		$last_opened_node = '';
		foreach ( $yaml as $line ) {
			if ( substr($line,0,3) == " - " ) {
				$list[$last_opened_node][] = substr($line,3);
			}
			else {
				$last_opened_node = substr($line,0,-1); // because last char is ':'
			}
		}
		return $list;
	}

	/**
	 * @brief returns all skills
	 * @param[in] $lang desired language of the results
	 * @param[out] $list simple numbered array of all skills
	 *
	 * Used twice (once for every language) everytime a
	 * translation is built. The arrays are later combined.
	 */
	public static function all_flat($lang=LANG) {
		$file = 'data/' . $lang . '/skills.yaml';
		$yaml = explode("\n", file_get_contents($file));

		$list = array();
		foreach ( $yaml as $line ) {
			if ( substr($line,0,3) == " - " ) {
				$list[] = substr($line,3);
			}
		}
		return $list;
	}
}
?>